package vip.xiaonuo.facedoor.remote.iservice.adapter;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import vip.xiaonuo.core.context.constant.ConstantContextHolder;
import vip.xiaonuo.core.pojo.httpclient.ReturnJson;
import vip.xiaonuo.facedoor.iservice.IFaceDoorDtoService;
import vip.xiaonuo.facedoor.modular.device.entity.DeviceCategory;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberfeatures.entity.MemberFeatures;
import vip.xiaonuo.facedoor.modular.memberfeatures.service.MemberFeaturesService;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.service.MemberUserService;


import java.util.List;
import java.util.Optional;

import vip.xiaonuo.facedoor.rule.DwRulesEngine;
import vip.xiaonuo.facedoor.vo.dewo.AddPersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.DeletePersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.GetPersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.GetPersonResponse;
import vip.xiaonuo.facedoor.vo.dewo.UpdatePersonRequest;

import javax.annotation.Resource;

@Component
public class MemberReqAdapter {

    public static final String KEY = "ABCD1234";


    @Autowired
    private MemberUserService memberUserService;

    @Resource
    private MemberFeaturesService memberFeaturesService;

    public ReturnJson deletePerson(MemberDevice memberDevice, String uri) throws Exception {
        DeletePersonRequest deletePersonRequest = new DeletePersonRequest();
        MemberUser byId = memberUserService.getById(memberDevice.getMemberId());
        deletePersonRequest.setMemberName(byId.getName());
        deletePersonRequest.setKey(KEY).setPersonnelId("1" + memberDevice.getMemberId().toString()).setMemberOrVisitorDeviceId(memberDevice.getId()).setMemberType(1);
        return DwRulesEngine.faceDoorDtoService.deletePerson(uri, deletePersonRequest);
    }

    public ReturnJson deleteAllPerson(String uri) throws Exception {
        DeletePersonRequest deletePersonRequest = new DeletePersonRequest();
        deletePersonRequest.setKey(KEY).setDeleteAll("1");
        return DwRulesEngine.faceDoorDtoService.deleteAllPerson(uri, deletePersonRequest);
    }

    public GetPersonResponse getPerson(String uri, MemberUser memberUser) throws Exception {
        GetPersonRequest getPersonRequest = new GetPersonRequest().setKey(KEY).setQueryType("1").setPersonnelId("1" + memberUser.getId().toString());
        return DwRulesEngine.faceDoorDtoService.getPerson(uri, getPersonRequest);
    }

    public ReturnJson addPerson(String uri, MemberUser memberUser) throws Exception {
        AddPersonRequest addPersonRequest = new AddPersonRequest().setKey(KEY)
                .setPersonnelId("1" + memberUser.getId().toString()).setName(memberUser.getName())
                .setIdCardNum(memberUser.getIdCard()).setCompany(memberUser.getCompanyName())
                .setLockerData(memberUser.getLockerData())
                .setFloors(memberUser.getFloors())
                .setIcNum(memberUser.getIcCard()).setJobNum(memberUser.getJobNum())
                .setWiegandNum(memberUser.getWiegandNum()).setPersonnelType(Optional.ofNullable(memberUser.getPersonType()).orElse(0) + "")
                .setMemberOrVisitorDeviceId(memberUser.getMemberOrVisitorDeviceId()).setMemberType(memberUser.getMemberType());

        // 湖山数字哨兵 定制
        Boolean hushan_shuzi_shaobing = ConstantContextHolder.getSysConfig("HUSHAN_SHUZI_SHAOBING", Boolean.class, false);
        // 黑名单身份信息脱敏
        if (hushan_shuzi_shaobing != null && hushan_shuzi_shaobing && ObjectUtil.equal(addPersonRequest.getPersonnelType(), "2")) {
            if (StringUtils.isNotBlank(memberUser.getName())) {
                addPersonRequest.setName(memberUser.getName().charAt(0) + "");
            }
            if (StringUtils.isNotBlank(memberUser.getIdCard())) {
                addPersonRequest.setIdCardNum(memberUser.getIdCard().substring(0, 6) + memberUser.getIdCard().substring(memberUser.getIdCard().length() - 4));
            }
        }

        if (!StringUtils.isEmpty(memberUser.getProperValue())) {
            // 如果特征为不为空，那么只传特征，不用传图片，以此减少终端的工作
            addPersonRequest.setFaceFeature(memberUser.getProperValue());
        } else {
            addPersonRequest.setFaceFeatureImgPath(memberUser.getPhoto());
        }

        // 查询是否有掌静脉特征值
        MemberFeatures features = memberFeaturesService.getOne(new LambdaQueryWrapper<MemberFeatures>()
                .eq(MemberFeatures :: getDwMemberUserId , memberUser.getId())
                .eq(MemberFeatures :: getPlatform , DeviceCategory.SUB_TYPE_STORE_PALM));
        if(ObjectUtil.isNotNull(features)){
            addPersonRequest.setPalmFeature(features.getValue());
        }

        return DwRulesEngine.faceDoorDtoService.addPerson(uri, addPersonRequest);
    }

    public ReturnJson updatePerson(String uri, MemberUser memberUser) throws Exception {
        UpdatePersonRequest updatePersonRequest = new UpdatePersonRequest();
        updatePersonRequest.setKey(KEY).setPersonnelId("1" + memberUser.getId().toString())
                .setLockerData(memberUser.getLockerData())
                .setFloors(memberUser.getFloors())
                .setName(memberUser.getName()).setIdCardNum(memberUser.getIdCard())
                .setCompany(memberUser.getCompanyName()).setIcNum(memberUser.getIcCard()).setJobNum(memberUser.getJobNum())
                .setWiegandNum(memberUser.getWiegandNum()).setMemberOrVisitorDeviceId(memberUser.getMemberOrVisitorDeviceId())
                .setMemberType(memberUser.getMemberType()).setPersonnelType(Optional.ofNullable(memberUser.getPersonType()).orElse(0) + "");
        updatePersonRequest.setFaceFeatureImgPath(memberUser.getPhoto());
        // 湖山数字哨兵 定制
        // 黑名单身份信息脱敏
        Boolean hushan_shuzi_shaobing = ConstantContextHolder.getSysConfig("HUSHAN_SHUZI_SHAOBING", Boolean.class, false);
        if (hushan_shuzi_shaobing != null && hushan_shuzi_shaobing && ObjectUtil.equal(updatePersonRequest.getPersonnelType(), "2")) {
            if (StringUtils.isNotBlank(memberUser.getName())) {
                updatePersonRequest.setName(memberUser.getName().charAt(0) + "");
            }
            if (StringUtils.isNotBlank(memberUser.getIdCard())) {
                updatePersonRequest.setIdCardNum(memberUser.getIdCard().substring(0, 6) + memberUser.getIdCard().substring(memberUser.getIdCard().length() - 4));
            }
        }

        // 查询是否有掌静脉特征值
        MemberFeatures features = memberFeaturesService.getOne(new LambdaQueryWrapper<MemberFeatures>()
                .eq(MemberFeatures :: getDwMemberUserId , memberUser.getId())
                .eq(MemberFeatures :: getPlatform , DeviceCategory.SUB_TYPE_STORE_PALM));
        if(ObjectUtil.isNotNull(features)){
            updatePersonRequest.setPalmFeature(features.getValue());
        }

        return DwRulesEngine.faceDoorDtoService.updatePerson(uri, updatePersonRequest);
    }
}
